import type * as T from './type'
import http from '@/utils/http'

export type * from './type'

const BASE_URL = '/system/user'

/** @desc 查询用户列表 */
export function listUser(query: T.UserPageQuery) {
  return http.get<PageRes<T.UserResp[]>>(`${BASE_URL}`, query)
}

/** @desc 查询用户详情 */
export function getUser(id: string) {
  return http.get<T.UserDetailResp>(`${BASE_URL}/${id}`)
}

/** @desc 新增用户 */
export function addUser(data: any) {
  return http.post(`${BASE_URL}/addUser`, data)
}

/** @desc 修改用户 */
export interface UpdateUserReq {
  id: number
  realname: string
  phone: string
  email: string
  avatar?: string
  roleIds: number[]
  status: number
}

export function updateUser(data: UpdateUserReq) {
  return http.post(`${BASE_URL}/updateUser`, data)
}

/** @desc 删除用户 */
export function deleteUser(id: string) {
  return http.del(`${BASE_URL}/delUser`, { id })
}

/** @desc 导出用户 */
export function exportUser(query: T.UserQuery) {
  return http.download(`${BASE_URL}/export`, query)
}

/** @desc 重置密码 */
export interface UserPasswordResetReq {
  id: number
  newPassword: string
}

export function resetUserPwd(data: UserPasswordResetReq) {
  return http.post(`${BASE_URL}/resetPwd`, data)
}

/** @desc 下载用户导入模板 */
export function downloadImportUserTemplate() {
  return http.download(`${BASE_URL}/downloadImportUserTemplate`)
}

/** @desc 解析用户导入数据 */
export function parseImportUser(data: FormData) {
  return http.post(`${BASE_URL}/parseImportUser`, data)
}

/** @desc 导入用户 */
export function importUser(data: any) {
  return http.post(`${BASE_URL}/import`, data)
}

/** @desc 批量导入用户（简化版） */
export interface BatchImportUserResp {
  successfulCount: number
  failureCount: number
  failedMobiles: string[]
}

export function batchImportUser(file: File) {
  const formData = new FormData()
  formData.append('file', file)
  return http.post<BatchImportUserResp>('/user/import', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/** @desc 获取当前用户信息（POST） */
export function getCurrentUserInfo() {
  return http.post('/user/userinfo')
}

export interface UserDetailResp {
  id: number
  realname: string
  phone: string
  email: string
  avatar: string
  gender: number
  status: number
  isSystem: boolean
  roles: Array<{ id: number }>
}
